﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace HelloASPX
{
    public partial class SelectProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                HttpContext.Current.Response.Write("<script>alert('请先登陆！');location='Login.aspx'</script>");
            }
        }

        protected void SelectProductList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/products.xml"));
            XmlNode productnode = doc.SelectSingleNode("//product[pid='" + SelectProductList.SelectedValue + "']");
            if (productnode != null)
            {
                productName.Text = productnode.SelectSingleNode("pname").InnerText;
                productTime.Text = productnode.SelectSingleNode("ptime").InnerText;
                productPrice.Text = productnode.SelectSingleNode("pprice").InnerText;
                productInfo.Text = productnode.SelectSingleNode("pinfo").InnerText;
                productPhoto.ImageUrl = productnode.SelectSingleNode("pimage").InnerText;
                Panel1.Visible = true;
            }
            else
            {
                Panel1.Visible = false;
            }
        }

        protected void UpdateProduct_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/products.xml"));
            XmlNode productnode = doc.SelectSingleNode("//product[pid='" + SelectProductList.SelectedValue + "']");
            if (productnode != null)
            {
                productnode.SelectSingleNode("pname").InnerText = productName.Text;
                productnode.SelectSingleNode("ptime").InnerText = productTime.Text;
                productnode.SelectSingleNode("pprice").InnerText = productPrice.Text;
                productnode.SelectSingleNode("pinfo").InnerText = productInfo.Text;
                if (productPhoto.ImageUrl != "")
                {
                    string photopath = "~/img/products/" + productImage1.FileName;
                    productnode.SelectSingleNode("pimage").InnerText = photopath;
                }
                doc.Save(Server.MapPath("~/App_Data/products.xml"));
                Response.Write("<script>alert('修改商品信息成功！');</script>");
            }
            else
            {
                Panel1.Visible = false; 
                Response.Write("<script>alert('修改商品信息失败！');</script>");
            }
        }

        protected void DeleteProduct_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/products.xml"));
            XmlNode productnode = doc.SelectSingleNode("//product[pid='" + SelectProductList.SelectedValue + "']");
            productnode.ParentNode.RemoveChild(productnode);
            try
            {
                doc.Save(Server.MapPath("~/App_Data/products.xml"));
                Response.Write("<script>alert('删除成功！');</script>");
            }
            catch
            {
                Response.Write("<script>alert('删除失败！');</script>");
            }
        }
    }
}